package com.hanhai.material.wages.controller;

import com.hanhai.material.common.result.Result;
import com.hanhai.material.common.result.ResultTool;
import com.hanhai.material.wages.entity.AttendanceResultReport;
import com.hanhai.material.wages.entity.DingAttendanceResult;
import com.hanhai.material.wages.service.AttendanceResultReportService;
import com.hanhai.material.wages.service.DingAttendanceResultService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.hanhai.material.utils.TimeUtils.*;
import static com.hanhai.material.wages.entity.table.AttendanceResultReportTableDef.ATTENDANCE_RESULT_REPORT;
import static com.hanhai.material.wages.entity.table.DingAttendanceResultTableDef.DING_ATTENDANCE_RESULT;

/**
 * 控制层。
 *
 * @author youchuang
 * @since 2023/10/25 17:34:51
 */
@RestController
@RequestMapping("/ding_attendance_result")
@Log4j2
public class DingAttendanceResultController {

    @Autowired
    private DingAttendanceResultService dingAttendanceResultService;

    @Autowired
    private AttendanceResultReportService attendanceResultReportService;

    /**
     * 添加。
     *
     * @param dingAttendanceResult
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    public boolean save(@RequestBody DingAttendanceResult dingAttendanceResult) {
        return dingAttendanceResultService.save(dingAttendanceResult);
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return dingAttendanceResultService.removeById(id);
    }

    /**
     * 根据主键更新。
     *
     * @param dingAttendanceResult
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    public boolean update(@RequestBody DingAttendanceResult dingAttendanceResult) {
        return dingAttendanceResultService.updateById(dingAttendanceResult);
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public List<DingAttendanceResult> list() {
        return dingAttendanceResultService.list();
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    public DingAttendanceResult getInfo(@PathVariable Serializable id) {
        return dingAttendanceResultService.getById(id);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<DingAttendanceResult> page(Page<DingAttendanceResult> page) {
        return dingAttendanceResultService.page(page);
    }

    @PostMapping("/get")
    public Result<?> getData(@RequestBody DingAttendanceResult dingAttendanceResult) {
        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper = QueryWrapper.create();
        queryWrapper.where(ATTENDANCE_RESULT_REPORT.ATTENDANCEDATE.eq(dingAttendanceResult.getWorkDate()));
        queryWrapper.where(ATTENDANCE_RESULT_REPORT.USERID.eq(dingAttendanceResult.getUserId()));
        List<AttendanceResultReport> attendanceResultReportList = this.attendanceResultReportService.list(queryWrapper);
//        for (AttendanceResultReport attendanceResultReport : attendanceResultReportList) {
//            queryWrapper = QueryWrapper.create();
//            queryWrapper.where(DING_ATTENDANCE_RESULT.USER_ID.eq(dingAttendanceResult));
////            queryWrapper.where(DING_ATTENDANCE_RESULT.WORK_DATE.between(dingAttendanceResult.getStartDate(),dingAttendanceResult.getEndDate()));
//            queryWrapper.where(DING_ATTENDANCE_RESULT.WORK_DATE.eq(fromDateToString("yyyy-MM-dd", attendanceResultReport.getAttendancedate())));
//            queryWrapper.orderBy(DING_ATTENDANCE_RESULT.BASE_CHECK_TIME, true);
//            List<DingAttendanceResult> dingAttendanceResultList = this.dingAttendanceResultService.list(queryWrapper);
////            attendanceResultReport.set
//        }


        return ResultTool.fail();
    }

    @PostMapping("/getToday/{userId}")
    public Result<?> getToday(@PathVariable String userId) {
        log.error("UserID:{}", userId);
        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper.where(DING_ATTENDANCE_RESULT.USER_ID.eq(userId));
        queryWrapper.where(DING_ATTENDANCE_RESULT.WORK_DATE.eq(fromDateToString("yyyy-MM-dd", new Date())));
        queryWrapper.orderBy(DING_ATTENDANCE_RESULT.BASE_CHECK_TIME, true);
        List<DingAttendanceResult> dingAttendanceResultList = this.dingAttendanceResultService.list(queryWrapper);
        return dingAttendanceResultList.isEmpty() ? ResultTool.fail() : ResultTool.success(dingAttendanceResultList);
    }

    @PostMapping("/getWeek/{userId}")
    public Result<?> getWeek(@PathVariable String userId) {
        List<DingAttendanceResult> dingAttendanceResultList = new ArrayList<>();
        List<LocalDate> localDateList = getDatesWeek();
        for (LocalDate date : localDateList) {
            DingAttendanceResult dingAttendanceResult = new DingAttendanceResult();
            QueryWrapper queryWrapper = QueryWrapper.create();
            queryWrapper.where(DING_ATTENDANCE_RESULT.USER_ID.eq(userId));
            queryWrapper.where(DING_ATTENDANCE_RESULT.WORK_DATE.eq(date));
            queryWrapper.orderBy(DING_ATTENDANCE_RESULT.BASE_CHECK_TIME, true);
            List<DingAttendanceResult> dingAttendanceResults = this.dingAttendanceResultService.list(queryWrapper);
            dingAttendanceResult.setWorkDate(Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant()));
            dingAttendanceResult.setDingAttendanceResultList(dingAttendanceResults);
            dingAttendanceResultList.add(dingAttendanceResult);
            queryWrapper = QueryWrapper.create();
            queryWrapper.where(ATTENDANCE_RESULT_REPORT.ATTENDANCEDATE.eq(date));
            queryWrapper.where(ATTENDANCE_RESULT_REPORT.USERID.eq(userId));
            AttendanceResultReport attendanceResultReport = this.attendanceResultReportService.getOne(queryWrapper);
            dingAttendanceResult.setAttendanceResultReport(attendanceResultReport);
        }
        return dingAttendanceResultList.isEmpty() ? ResultTool.fail() : ResultTool.success(dingAttendanceResultList);

    }

    @PostMapping("/getMonth/{userId}")
    public Result<?> getMonth(@PathVariable String userId) {
        List<DingAttendanceResult> dingAttendanceResultList = new ArrayList<>();
        LocalDate localDate = LocalDate.now();
        YearMonth yearMonth = YearMonth.of(localDate.getYear(), localDate.getMonthValue());
        List<LocalDate> localDateList = getDatesOfMonth(yearMonth);

        for (LocalDate date : localDateList) {
            DingAttendanceResult dingAttendanceResult = new DingAttendanceResult();
            QueryWrapper queryWrapper = QueryWrapper.create();
            queryWrapper.where(DING_ATTENDANCE_RESULT.USER_ID.eq(userId));
            queryWrapper.where(DING_ATTENDANCE_RESULT.WORK_DATE.eq(date));
            queryWrapper.orderBy(DING_ATTENDANCE_RESULT.BASE_CHECK_TIME, true);
            List<DingAttendanceResult> dingAttendanceResults = this.dingAttendanceResultService.list(queryWrapper);
            dingAttendanceResult.setWorkDate(Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant()));
            dingAttendanceResult.setDingAttendanceResultList(dingAttendanceResults);
            dingAttendanceResultList.add(dingAttendanceResult);
            queryWrapper = QueryWrapper.create();
            queryWrapper.where(ATTENDANCE_RESULT_REPORT.ATTENDANCEDATE.eq(date));
            queryWrapper.where(ATTENDANCE_RESULT_REPORT.USERID.eq(userId));
            AttendanceResultReport attendanceResultReport = this.attendanceResultReportService.getOne(queryWrapper);
            dingAttendanceResult.setAttendanceResultReport(attendanceResultReport);
        }
        return dingAttendanceResultList.isEmpty() ? ResultTool.fail() : ResultTool.success(dingAttendanceResultList);

    }
}
